package com.yzl.meeting.framework.beans;

/**
 * @Title: PropertyValueResolver
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/21 19:04
 * @Version: 1.0
 */

import com.yzl.meeting.framework.beans.factory.BeanFactory;
import com.yzl.meeting.framework.pojo.RuntimeBeanReference;
import com.yzl.meeting.framework.pojo.TypeStringValue;

/**
 * 用来解析property标签ref/value属性的属性值
 */
public class PropertyValueResolver {

    private BeanFactory beanFactory;

    public PropertyValueResolver(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    /**
     * 根据pvValue的具体类型返回相应的值
     * pvValue:ref对应类型    返回bean
     * pvVAlue:value对象类型  返回value属性的值本身
     * @param pvValue
     * @return
     */
    public Object resolveValue(Object pvValue) {
        if(pvValue instanceof RuntimeBeanReference){
            RuntimeBeanReference rbr = (RuntimeBeanReference)pvValue;
            String beanId = rbr.getBeanId();
            Object bean = beanFactory.getBean(beanId);
            return bean;
        }else if(pvValue instanceof TypeStringValue){
            TypeStringValue tsv = (TypeStringValue)pvValue;
            String value = tsv.getValue();
            return value;
        }
        return null;
    }
}